package com.uni.db;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import com.uni.domain.Group;
import com.uni.domain.Identifiable;
import com.uni.domain.User;

public class UserServiceImpl extends GenericServiceImpl<User> implements UserService{

	@SuppressWarnings("unchecked")
	@Override
	public User getByLogin(String login) {
		List<User> list = (List<User>)getSession().createCriteria(User.class).add(Restrictions.eq("login", login)).list();
		return list.size() > 0 ? list.get(0) : null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getStudentsWithGroup(Group group) {
		List<User> list = (List<User>)getSession()
		.createCriteria(User.class)
		.add(Restrictions.eq("student", true))
		.add(Restrictions.eq("gruop_id", group.getId())).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getStudentsWithoutGroup() {
		List<User> list = (List<User>)getSession()
		.createCriteria(User.class)
		.add(Restrictions.eq("student", true))
		.add(Restrictions.isNull("group")).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUsers(List<Long> ids) {
		return (List<User>)getSession().createCriteria(User.class).add(Restrictions.in("id", ids)).list();
	}
	
//	public void deleteOldPhoto(User user){
//		String hql = "DELETE FROM File WHERE k_id = :kid AND type = :type";
//	    Query query = dao.getSession().createQuery(hql);
//	    query.setLong("kid", entity.getId());
//	    query.setString("type", "photo");
//	}
	
}
